package com.example.design.time;

import com.lenovoconnect.common.timer.enginer.TimerEnginer;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;


@Slf4j
@SpringBootApplication
@SpringBootTest(classes = SpringTimerEnginerTest.class)
public class SpringTimerEnginerTest {
    @Resource(name = "springTimerEnginer")
    TimerEnginer timerEnginer;

    @Test
    public void defaultTimerEnginerTest() throws InterruptedException {
        // 初始化一个定时器
        TimeTaskHandlerTest timeTaskHandlerTest = new TimeTaskHandlerTest();
        timerEnginer.executeTask(timeTaskHandlerTest);


        Thread.sleep(10000);
        log.info("变更cron....");

        // 修改定时器的cron
        timeTaskHandlerTest.setCron("0/3 * * * * ?");
        timerEnginer.executeTask(timeTaskHandlerTest);
        Thread.sleep(10000);

        // 新增定时器
        TimeTaskHandlerTest2 timeTaskHandlerTest2 = new TimeTaskHandlerTest2();
        timerEnginer.executeTask(timeTaskHandlerTest2);
        Thread.sleep(20000);

    }

    @Slf4j
    public static class TimeTaskHandlerTest implements TimeTaskHandler {
        // 3秒 0/3 * * * * ?
        @Setter
        String cron = "0/2 * * * * ?";

        @Override
        public String taskName() {
            return "test";
        }

        @Override
        public String getCron() {
            return cron;
        }

        @Override
        public void run() {
            log.info("aaa");
        }
    }


    @Slf4j
    public static class TimeTaskHandlerTest2 implements TimeTaskHandler {
        // 3秒 0/3 * * * * ?
        @Setter
        String cron = "0/5 * * * * ?";

        @Override
        public String taskName() {
            return "test2";
        }

        @Override
        public String getCron() {
            return cron;
        }

        @Override
        public void run() {
            log.info("bbbb");
        }
    }

}
